package com.dingpeikeji.quickdeer.modules.rentproduct.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TCargotypeForsale;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentprodPrice;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TCargotypeForsaleService;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.TRentprodPriceService;

/**
 * 整车价格区间Controller
 * 
 * @author wuhao
 * @version 2016-08-08
 */
@Controller
@RequestMapping(value = "${adminPath}/rentprod/tRentprodPrice")
public class TRentprodPriceController extends BaseController {

	@Autowired
	private TRentprodPriceService tRentprodPriceService;
	
	@Autowired
	private TCargotypeForsaleService tCargotypeForsaleService;

	@ModelAttribute
	public TRentprodPrice get(@RequestParam(required = false) String rentpriceid) {
		TRentprodPrice entity = null;
		if (StringUtils.isNotBlank(rentpriceid)) {
			entity = tRentprodPriceService.get(rentpriceid);
		}
		if (entity == null) {
			entity = new TRentprodPrice();
		}
		return entity;
	}

	@RequiresPermissions("rentprod:tRentprod:view")
	@RequestMapping(value = { "list", "" })
	public String list(TRentprodPrice tRentprodPrice, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<TRentprodPrice> page = tRentprodPriceService.findPage(new Page<TRentprodPrice>(request, response),
				tRentprodPrice);
		model.addAttribute("page", page);
		model.addAttribute("tRentprodPrice", tRentprodPrice);
		return "modules/rentprod/tRentprodPriceList";
	}
	
	@RequiresPermissions("rentprod:tRentprod:allview")
	@RequestMapping(value = { "alllist" })
	public String alllist(TRentprodPrice tRentprodPrice, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<TRentprodPrice> page = tRentprodPriceService.findPage(new Page<TRentprodPrice>(request, response),
				tRentprodPrice);
		model.addAttribute("page", page);
		model.addAttribute("tRentprodPrice", tRentprodPrice);
		return "modules/rentprod/tRentprodPriceAllList";
	}

	@RequiresPermissions("rentproduct:tRentprodPrice:view")
	@RequestMapping(value = { "cartypelist" })
	public String cartypelist(TCargotypeForsale tCargotypeForsale, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		if (tCargotypeForsale.getOffice() == null)
			tCargotypeForsale.setOffice(UserUtils.getUser().getOffice());

		Page<TCargotypeForsale> page = tCargotypeForsaleService.findPage(new Page<TCargotypeForsale>(request, response),
				tCargotypeForsale);
		model.addAttribute("page", page);
		return "modules/rentproduct/tCargotypePriceList";
	}

	@RequiresPermissions("rentproduct:tRentprodPrice:view")
	@RequestMapping(value = "form")
	public String form(TRentprodPrice tRentprodPrice, Model model) {
		model.addAttribute("tRentprodPrice", tRentprodPrice);
		return "modules/rentproduct/tRentprodPriceForm";
	}

	@RequiresPermissions("rentproduct:tRentprodPrice:edit")
	@RequestMapping(value = "save")
	public String save(TRentprodPrice tRentprodPrice, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tRentprodPrice)) {
			return form(tRentprodPrice, model);
		}
		tRentprodPriceService.save(tRentprodPrice);
		addMessage(redirectAttributes, "保存整车价格区间成功");
		return "redirect:" + Global.getAdminPath() + "/rentproduct/tRentprodPrice/?repage";
	}

	@RequiresPermissions("rentproduct:tRentprodPrice:edit")
	@RequestMapping(value = "delete")
	public String delete(TRentprodPrice tRentprodPrice, RedirectAttributes redirectAttributes) {
		tRentprodPriceService.delete(tRentprodPrice);
		addMessage(redirectAttributes, "删除整车价格区间成功");
		return "redirect:" + Global.getAdminPath() + "/rentproduct/tRentprodPrice/?repage";
	}

}